package com.hb.trade.api.routine.controller.project.complaint;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.center.sys.dto.NewsCenterDTO;
import com.hb.core.pojo.enums.NewsTypeEnum;
import com.hb.core.pojo.enums.NewsTypeTwoEnum;
import com.hb.core.pojo.project.complaint.dto.ComplaintDTO;
import com.hb.core.pojo.project.complaint.dto.ComplaintReplyDTO;
import com.hb.core.pojo.project.complaint.entity.ComplaintReply;
import com.hb.core.pojo.project.proj.entity.AbstractProject;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.center.NewsCenterClient;
import com.hb.trade.client.project.ComplaintClient;
import com.hb.trade.client.project.ProjectClient;
import com.hb.trade.client.project.SectionClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 投诉异议Api接口
 *
 * @author ljs
 * @date 2022-1-26 16:11:38
 */
@Slf4j
@Version(1.0)
@Api(tags = {"投诉异议"})
@RequestMapping("/{version}")
@RestController
public class ApiComplaintController {

    protected final ComplaintClient complaintClient;
    protected final NewsCenterClient newsCenterClient;
    protected final SectionClient sectionClient;
    protected final ProjectClient projectClient;

    public ApiComplaintController(ComplaintClient complaintClient,
                                  NewsCenterClient newsCenterClient,
                                  SectionClient sectionClient,
                                  ProjectClient projectClient) {
        this.complaintClient = complaintClient;
        this.newsCenterClient = newsCenterClient;
        this.sectionClient = sectionClient;
        this.projectClient = projectClient;
    }

    @ApiOperation(value = "保存异议接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionCode", value = "标段code", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sectionName", value = "标段名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintTitle", value = "投诉标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintContent", value = "投诉内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "attachment", value = "投诉附件", dataTypeClass = Attachments.class),
            @ApiImplicitParam(name = "complaintName", value = "投诉/举报人", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintPhone", value = "投诉/举报人联系电话", dataTypeClass = String.class),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class)
    })
    @PostMapping("/complaint/save")
    public ResponseDto saveComplaint(@RequestBody ComplaintDTO complaintDTO) {
        Long orgId = ShiroUtil.getOrgId();
        complaintDTO.setSubmitOrgId(String.valueOf(orgId));
        ShiroUtil.fillOwner(complaintDTO);
        Message result = complaintClient.saveComplaint(complaintDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "提交异议", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异议ID", dataTypeClass = Long.class)
    })
    @PostMapping("/complaint/commitComplaint")
    public ResponseDto commitComplaint(@RequestBody ComplaintDTO complaintDTO) {
        Message result = complaintClient.commitComplaint(complaintDTO);
        Message<ComplaintDTO> message = complaintClient.complaintDetail(complaintDTO);
        if (message.isFail() || message.getData() == null){
            return ResultUtil.msg(result);
        }
        //给招标人发送消息
        complaintDTO = message.getData();

        //给招标人发送审核消息
        SendNewsUtil.sendTendereeNews(
                complaintDTO.getSectionId(),
                NewsTypeEnum.OBJECTION_QUESTION,
                NewsTypeTwoEnum.TENDEREEFILING_OBJECTION,
                null,
                "/bidder/complaintsobjection"
        );
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "异议结束", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异议ID", dataTypeClass = Long.class)
    })
    @PostMapping("/complaint/finishComplaint")
    public ResponseDto finishComplaint(@RequestBody ComplaintDTO complaintDTO) {
        Message result = complaintClient.finishComplaint(complaintDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "招标人查询投诉异议", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "当前用户的企业ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class)
    })
    @PostMapping("/complaint/pageComplaint")
    public ResponseDto pageComplaint(@RequestBody ComplaintDTO complaintDTO) {
        Long orgId = ShiroUtil.getOrgId();
        complaintDTO.setSubmitOrgId(String.valueOf(orgId));
        Message result = complaintClient.pageComplaint(complaintDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "投标人查询投诉异议", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "当前用户的企业ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class)
    })
    @PostMapping("/complaint/pageTenderComplaint")
    public ResponseDto pageTenderComplaint(@RequestBody ComplaintDTO complaintDTO) {
        Long orgId = ShiroUtil.getOrgId();
        complaintDTO.setSubmitOrgId(String.valueOf(orgId));
        ShiroUtil.fillOwner(complaintDTO);
        Message result = complaintClient.pageTenderComplaint(complaintDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "招标人回复异议保存", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "complaintId", value = "投诉异议ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "replyContent", value = "回复正文", dataTypeClass = String.class),
            @ApiImplicitParam(name = "replyAttachment", value = "附件", dataTypeClass = Attachments.class)
    })
    @PostMapping("/complaintReply/save")
    public ResponseDto saveComplaintReply(@RequestBody ComplaintReplyDTO complaintReplyDTO) {
        Message result = complaintClient.saveComplaintReply(complaintReplyDTO);

        //给投标人发送回复的消息
        ComplaintDTO complaintDTO = new ComplaintDTO();
        complaintDTO.setId(complaintReplyDTO.getComplaintId());
        Message complaintDetailMsg = complaintClient.complaintDetail(complaintDTO);
        if (complaintDetailMsg.getData() == null){
            return ResultUtil.msg(result);
        }
        complaintDTO = (ComplaintDTO) complaintDetailMsg.getData();

        //给招标人发送审核消息
        SendNewsUtil.sendBiddereeNews(
                complaintDTO.getSectionId(),
                NewsTypeEnum.OBJECTION_QUESTION,
                NewsTypeTwoEnum.BIDDER_REPLY_OBJECTION,
                null,
                null,
                "/bidder/complaintsobjection",
                CollUtil.list(false,complaintDTO.getCreateId())
        );
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "查看投诉异议详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionCode", value = "标段code", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sectionName", value = "标段名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintTitle", value = "投诉标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintContent", value = "投诉内容", dataTypeClass = String.class),
            @ApiImplicitParam(name = "attachment", value = "投诉附件", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintName", value = "投诉/举报人", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintPhone", value = "投诉/举报人联系电话", dataTypeClass = String.class),
            @ApiImplicitParam(name = "projectCode", value = "项目编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "complaintReply", value = "异议回复信息", dataTypeClass = ComplaintReply.class)
    })
    @PostMapping("/complaint/complaintDetail")
    public ResponseDto complaintDetail(@RequestBody ComplaintDTO complaintDTO) {
        Message result = complaintClient.complaintDetail(complaintDTO);
        return ResultUtil.msg(result);
    }

}
